import twData from './taiwan.json'
import {
	formatCity
} from './filterCity'
export default {
	/**
	 * 按省的城市编码排序
	 */
	orderByCityCode(a, b) {
		return parseInt(a.value) - parseInt(b.value)
	},
	/**
	 * 自定义app数据格式
	 * @param {Array} addressList 高德地图api返回的城市数据
	 * @param {boolean} noFilter 是否过滤省市区
	 * @param {boolean} allProvice 是否显示全省
	 * @param {boolean} allCity 是否显示全市
	 * @param {boolean} allCountry 是否显示全国
	 */
	buildAppJSONData({
		addressList,
		noFilter = false,
		allProvince = false,
		allCity = false,
		allCountry = false,
	}) {
		let array = []
		get(addressList, array, noFilter)

		array.forEach(item => {
			if (item.value === '710000') {
				if (item.children.length === 0) {
					item.children = twData
				}
			}
		})
		array.sort(this.orderByCityCode) // 加入台湾区域，并按省份城市代码排序

		const specialCitys = ['北京', '上海', '天津', '重庆', '香港', '澳门']
		if (array.length > 0) {
			array.forEach(item => {
				if (allCity) {
					item.children.forEach(city => {
						city.children.unshift({
							value: '0',
							label: '全市',
							children: []
						})
					})
				}
				if (allProvince) {
					if (specialCitys.indexOf(item.label) === -1) {
						item.children.unshift({
							value: '0',
							label: '全省',
							children: [{
								value: '0',
								label: '全省',
								children: []
							}]
						})
					}
				}
			})
			if (allCountry) {
				array.unshift({
					value: '0',
					label: '全国',
					children: [{
						value: '0',
						label: '全国',
						children: [{
							value: '0',
							label: '全国',
							children: []
						}]
					}]
				})
			}
		}
		return array

		function get(districts, children, noFilter) {
			districts.forEach((item, index) => {
				children.push({
					value: noFilter === true ? item.adcode : item.name,
					label: noFilter === true ? formatCity(item.name) : item.name
				})
				if (item.districts.length != 0) {
					children[children.length - 1].children = []
					if (item.adcode === item.districts[0].adcode) {
						children[children.length - 1].children = [{
							value: noFilter === true ? item.adcode : item.name,
							label: noFilter === true ? formatCity(item.name) : item.name
						}]
					} else {
						get(item.districts, children[children.length - 1].children, noFilter)
					}
				} else {
					// 特别处理香港，市区统一
					if (parseInt(item.adcode) > 810000 && parseInt(item.adcode) < 820000) {
						children[children.length - 1].children = [{
							value: noFilter === true ? item.adcode : item.name,
							label: noFilter === true ? formatCity(item.name) : item.name
						}]
					}
					// 特别处理澳门，市区统一
					if (parseInt(item.adcode) > 820000 && parseInt(item.adcode) < 830000) {
						children[children.length - 1].children = [{
							value: noFilter === true ? item.adcode : item.name,
							label: noFilter === true ? formatCity(item.name) : item.name
						}]
					}
					// 特别处理台湾
					if (parseInt(item.adcode) === 710000) {
						children[children.length - 1].children = []
					}
				}
			})
		}
	},
	getGaoDeData(key) {
		if (key) {
			return new Promise((resolve, reject) => {
				uni.request({
					method: 'get',
					// url: `https://restapi.amap.com/v3/config/district?subdistrict=3&key=${key}`,
					url: `https://ec.lslad.com/addr.json`,
					success: res => {
						if (res.statusCode === 200) {
							// 构造匹配app，otms框架的城市JSON文件
							// const data = this.buildAppJSONData(, false, false)
							resolve(res.data.districts[0].districts)
						} else {
							reject(res)
						}
					},
					fail: res => {
						reject(res)
					}
				})
			})
		} else {
			uni.showModal({
				title: '提示',
				content: '请输入高德key'
			})
		}
	}
}
